package com.ca.ui.core.view.list;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ui.core.R;
import com.ca.ui.core.bean.ClubInfo;
import com.ca.ui.core.util.NumberUtils;
import com.ca.ui.core.view.card.InfoCard;

import java.util.ArrayList;
import java.util.List;

public class FollowClubListView extends ConstraintLayout {

    private final List<InfoCard> cardList = new ArrayList<>();

    public TextView emptyView;
    private List<ClubInfo> clubInfoList;

    public FollowClubListView(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public FollowClubListView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public FollowClubListView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {

        View view = LayoutInflater.from(context).inflate(R.layout.follow_club_list_layout, this);

        emptyView = view.findViewById(R.id.tv_empty);

        cardList.add(view.findViewById(R.id.card_miniInfo_1));
        cardList.add(view.findViewById(R.id.card_miniInfo_2));
        cardList.add(view.findViewById(R.id.card_miniInfo_3));
        cardList.add(view.findViewById(R.id.card_miniInfo_4));

        setInfoList(clubInfoList);
    }

    public List<ClubInfo> getInfoList() {
        return clubInfoList;
    }

    public void setInfoList(List<ClubInfo> clubInfoList) {

        this.clubInfoList = clubInfoList == null ? new ArrayList<>() : clubInfoList;

        emptyView.setVisibility(this.clubInfoList.size() == 0 ? VISIBLE : GONE);

        for (int i = 0; i < this.cardList.size(); i++) {

            InfoCard card = getInfoCard(i);

            if (i < this.clubInfoList.size()) {
                card.setEnabled(true);

                ClubInfo info = getInfo(i);
                card.setImageUrl(info.getImageUrl());
                card.setName(info.getName());
                String text = NumberUtils.numberToString((long) info.getPaperCount()) + "条动态";
                card.setOther(text);
            }
            else {
                card.setEnabled(false);
            }
        }
    }

    public InfoCard getInfoCard(int index) {
        return this.cardList.get(index);
    }

    public ClubInfo getInfo(int index) {
        return this.clubInfoList.get(index);
    }
}
